/**   
* @Title: StockController.java 
* @Package com.txby.platformApi.component.web 
* @Description: TODO(用一句话描述该文件做什么) 
* @author Administrator
* @date 2017年11月2日 下午4:07:49 
* @version V1.0   
*/
package com.txby.platformApi.component.web;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.jck.mybatis.util.PageList;
import com.jck.tools.ChkUtil;
import com.txby.pf.common.utils.PageUtils;
import com.txby.pf.common.utils.Query;
import com.txby.pf.common.utils.R;
import com.txby.pf.common.validator.ValidatorUtils;
import com.txby.pf.common.validator.group.AddGroup;
import com.txby.platformCore.entity.SysStock;

/** 
* @ClassName: StockController 
* @Description: 库存管理类 
* @author Administrator
* @date 2017年11月2日 下午4:07:49 
*  
*/
@RestController
@RequestMapping("/pro/stock")
public class StockController extends CommonController {

	
	@RequestMapping("/list")
	@RequiresPermissions("pro:stock:info")
	public R list(@RequestParam Map<String, Object> params){
//		数据隔离
		String join="";
		String sql="SELECT t.*,u.username FROM  "+this.mBaseDao.tableName(SysStock.class)+" t , sys_user u WHERE t.checkMan=u.user_id " ;
		String deviceName=params.get("deviceName")==null?"":params.get("deviceName").toString();
		if(!ChkUtil.isEmptyAllObject(deviceName)){
			if (deviceName.contains("%") || deviceName.contains("_")) {
				deviceName = deviceName.trim().replace("%", "\\%").replace("_", "\\_");
				deviceName = "%" + deviceName + "%";
				sql += " and t.deviceName like #{pojo.deviceName} escape '\\'";
				params.put("deviceName", "%" + deviceName.trim() + "%");
			} else {
				deviceName = "%" + deviceName.trim() + "%";
				sql += " and t.deviceName like #{pojo.deviceName} ";
				params.put("deviceName", "%" + deviceName.trim() + "%");
			}
		}
		sql += Query.getLastSortSql(params, " order by updateTime ");
		//查询列表数据
		PageList<Map> pl = this.mBaseDao.findBySqlForPageList(sql, Map.class, Query.getPageBean(params), params);
		PageUtils pageUtil = new PageUtils(pl.getResultList(), pl.getPageBean().getTotalCount().intValue(),  pl.getPageBean().getPageSize(), pl.getPageBean().getPage());
		return R.ok().put("page", pageUtil);
	}
	
	/**
	 * 查询物料编码及设备型号
	 */
	@RequestMapping("/queryMaterialCode")
	public R queryMaterialCode(@RequestParam Map<String, Object> params) {
		String deviceType=(String) params.get("deviceType");
		String sql="SELECT materialCode text,materialCode value  FROM  "+this.mBaseDao.tableName(SysStock.class)+" t WHERE deviceType='"+deviceType+"'" ;
		String typeSql="SELECT typeNo text,typeNo value FROM  "+this.mBaseDao.tableName(SysStock.class)+" t WHERE deviceType='"+deviceType+"'" ;
		List list=mBaseDao.findBySql(sql, Map.class, null);
		List typeList=mBaseDao.findBySql(typeSql, Map.class, null);
		return R.ok().put("mcList", list).put("typeNoList", typeList);
	}
	
	/**
	 * 查询信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("pro:stock:info")
	public R info(@PathVariable("id") String id){
		String sql="SELECT t.*  FROM  "+this.mBaseDao.tableName(SysStock.class)+" t WHERE  t.id='"+id+"'" ;
		Map stock =  (Map) this.mBaseDao.findBySqlUniqueObject(sql, Map.class, null);
		return R.ok().put("stock", stock);
	}
	
	/**
	 * 保存库存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("pro:stock:save")
	public R save(@RequestBody SysStock stock){
		try {
			ValidatorUtils.validateEntity(stock);
			String sql="select * from "+this.mBaseDao.tableName(SysStock.class)+" where typeNo='"+stock.getTypeNo()+"' and deviceType='"+stock.getDeviceType()+"'";
			SysStock old=(SysStock) this.mBaseDao.findBySqlUniqueObject(sql, SysStock.class, null);
			 if(old!=null){
				 return R.ok().error("该类型的设备型号重复");
			 }
			 stock.setCreater(getUserId().toString());
			 stock.setUpdater(getUserId().toString());
			 stock.setCheckMan(getUserId().toString());
			this.mBaseDao.saveObject(stock);
		} catch (Exception e) {
			return R.error(5000,e.getMessage());
		}
		return R.ok();
	}
	
	/**
	 * 修改库存
	 */
	@RequestMapping("/update")
	@RequiresPermissions("pro:stock:update")
	public R update(@RequestBody  SysStock stock){
		try {
			ValidatorUtils.validateEntity(stock, AddGroup.class);
			String sql="select * from "+this.mBaseDao.tableName(SysStock.class)+" where typeNo ="+stock.getTypeNo()+" and id_!="+stock.getId_()+" ";
			SysStock old=(SysStock) this.mBaseDao.findBySqlUniqueObject(sql, SysStock.class, null);
			if(old!=null){
				return R.ok().error("该类型的设备型号重复");
			}else{
				old=(SysStock) this.mBaseDao.findBySqlUniqueObject("select * from "+this.mBaseDao.tableName(SysStock.class)+" where   id_="+stock.getId_(), SysStock.class, null);
				if(old!=null){
					this.mBaseDao.updateObject(stock);
				}
			}
			
		}catch (Exception e) {
			return R.error(5000,e.getMessage());
		}
		return R.ok();
	}
	
	
	/**
	 * 删除库存
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("pro:stock:delete")
	public R delete(@RequestBody String[] ids){
		 for(String id:ids){
		    	this.mBaseDao.executeSql(" delete from "+this.mBaseDao.tableName(SysStock.class)+" where id='"+id+"'", null);
		  }
		return R.ok();
	}
}
